package inet.sms.network;

import java.io.FileInputStream;
import java.io.IOException;
import java.io.PrintStream;
import java.util.Collections;
import java.util.Enumeration;
import java.util.List;
import java.util.Properties;
import sms.util.Encrypter;

public class NetworkConfig {
	public static String IP_ADDRESS = null;
	public static String PASSWORD = null;
	public static int PORT = 0;
	public static String USERNAME = null;
	private static final String fileName = "./conf/network.conf";
	private static boolean monitorEnabled = false;
	private static boolean networkEnabled = false;
	private static Properties properties = new Properties();
	public static int TIME_REBIND = 0;

	public static String debugString(String param) {
		if (param != null)
			param = param.toLowerCase();
		String s = "NetworkConfig: network.conf\r\n";
		List keyList = Collections.list(properties.keys());
		Collections.sort(keyList);
		for (Enumeration sortedEnum = Collections.enumeration(keyList); sortedEnum
				.hasMoreElements();) {
			String key = (String) sortedEnum.nextElement();
			if (key == null)
				continue;
			key = key.toLowerCase();
			if ((param != null) && (!(key.startsWith(param))))
				continue;
			String val = null;
			if (key.indexOf("password") > 0)
				val = "*****";
			else
				val = (String) properties.get(key);
			s = s + key + " = " + val + "\r\n";
		}

		return s;
	}

	static byte getByteProperty(String propName, byte defaultValue) {
		return Byte.parseByte(properties.getProperty(propName,
				Byte.toString(defaultValue)).trim());
	}

	static int getIntProperty(String propName, int defaultValue) {
		return Integer.parseInt(properties.getProperty(propName,
				Integer.toString(defaultValue)).trim());
	}

	public static Properties getProperties() {
		return properties;
	}

	public static boolean isMonitorEnabled() {
		return monitorEnabled;
	}

	public static boolean isNetworkEnabled() {
		return networkEnabled;
	}

	private static void loadFileProperties() throws IOException {
		FileInputStream propsFile = new FileInputStream("./conf/network.conf");
		properties.load(propsFile);
		propsFile.close();
	}

	public static void loadProperties() throws IOException {
		loadFileProperties();
		updateValues();
	}

	public static void main(String[] args) {
		try {
			loadProperties();
			System.out.println(isNetworkEnabled());
			System.out.println(IP_ADDRESS);
		} catch (IOException localIOException) {
		}
	}

	private static void updateValues() {
		IP_ADDRESS = properties.getProperty("ip_address");
		PORT = getIntProperty("port", PORT);
		USERNAME = properties.getProperty("username");
		PASSWORD = properties.getProperty("password");
		if (PASSWORD == null)
			return;
		try {
			PASSWORD = Encrypter.decrypt(PASSWORD);
		} catch (Exception ex) {
			ex.printStackTrace();
			return;
		}

		int i = getIntProperty("network_enabled", 0);
		if (i == 1)
			networkEnabled = true;
		else
			networkEnabled = false;
		System.out.println("Network\t"
				+ ((isNetworkEnabled()) ? "enabled" : "disabled"));
		i = getIntProperty("monitor_enabled", 0);
		if (i == 1)
			monitorEnabled = true;
		else
			monitorEnabled = false;
		System.out.println("Monitor\t"
				+ ((isMonitorEnabled()) ? "enabled" : "disabled"));

		TIME_REBIND = getIntProperty("TIME_REBIND", TIME_REBIND);

		TIME_REBIND *= 60;
	}
}
